package net.zhengxinyun.performance.controller.measurement;

import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.MeasurementInformationEntity;
import net.zhengxinyun.performance.entity.QuestionEntity;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.filter.MeasurementInformationFilter;
import net.zhengxinyun.performance.filter.QuestionFilter;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.measurement.AssessmentTestService;
import net.zhengxinyun.performance.yunpian.YunpianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by hyp1202 on 2018/12/29.
 */
@RestController
@RequestMapping("/assessmentTest")
public class AssessmentTestController {

    private TokenService tokenService;
    private LoginService loginService;
    private AssessmentTestService assessmentTestService;
    private YunpianService yunpianService;

    @Autowired
    public AssessmentTestController(TokenService tokenService, LoginService loginService, AssessmentTestService assessmentTestService, YunpianService yunpianService) {
        this.tokenService = tokenService;
        this.loginService = loginService;
        this.assessmentTestService = assessmentTestService;
        this.yunpianService = yunpianService;
    }

    @PostMapping("/insert")
    public Result<String> insert(@RequestBody AssessmentTestParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = assessmentTestService.insert(param.measurementInformationEntity, param.questionEntityList);
            if (!"添加成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/update")
    public Result<String> update(@RequestBody AssessmentTestParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = assessmentTestService.update(param.measurementInformationEntity, param.questionEntityList);
            if (!"修改成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/delete")
    public Result<String> delete(@RequestBody AssessmentTestParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = assessmentTestService.delete(param.measurementInformationEntity);
            if (!"测评删除成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/query")
    public Result<HashMap<String,Object>> query(@RequestBody AssessmentTestParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            HashMap<String,Object> map = new HashMap<>();
            List<MeasurementInformationFilter> list = assessmentTestService.query(param.measurementInformationFilter);
            if (list.size() == 0){
                param.measurementInformationFilter.setPageSize(param.measurementInformationFilter.getPageSize()-1);
                list = assessmentTestService.query(param.measurementInformationFilter);
            }
            Integer pageTotal = assessmentTestService.queryForCount(param.measurementInformationFilter);
            map.put("list",list);
            map.put("pageTotal",pageTotal);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/queryForPaper")
    public Result<HashMap<String,Object>> queryForPaper(@RequestBody AssessmentTestParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            HashMap<String,Object> map = assessmentTestService.queryForPaper(param.measurementInformationFilter);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/queryForAnswer")
    public Result<ArrayList<MeasurementInformationFilter>> queryForAnswer(@RequestBody AssessmentTestParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            ArrayList<MeasurementInformationFilter> list = (ArrayList<MeasurementInformationFilter>) assessmentTestService.queryForAnswer(param.measurementInformationFilter);
            return ResultUtils.getSuccessResults(list);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/importMeasurement")
    public Result<String> importMeasurement(@RequestParam(value = "file")MultipartFile file){
        String str = assessmentTestService.importMeasurement(file);
        if (!"导入成功".equals(str)){
            return ResultUtils.getResults(Result.Status.ERROR,str);
        }
        return ResultUtils.getSuccessResults();
    }


}
class AssessmentTestParam{
    public TokenEntity tokenEntity;
    public MeasurementInformationEntity measurementInformationEntity;
    public QuestionFilter questionFilter;
    public MeasurementInformationFilter measurementInformationFilter;
    public QuestionEntity questionEntity;
    public List<QuestionEntity> questionEntityList;
}
